package com.example.livingadminmaster.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.livingadminmaster.common.IdWorker;
import com.example.livingadminmaster.mapper.DormitoryMapper;
import com.example.livingadminmaster.pojo.Dormitory;
import com.example.livingadminmaster.pojo.Faculty;
import com.example.livingadminmaster.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements DormitoryService {
    IdWorker idWorker = new IdWorker(1, 1);
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Override
    public List<Dormitory> getAllDormitories() {
        List<Dormitory> dormitories=dormitoryMapper.selectList(null);
        return dormitories;
    }

    @Override
    public Dormitory getDormitoryId(long dormitory_admin_id) {
        return dormitoryMapper.selectById(dormitory_admin_id);
    }

    @Override
    public Dormitory getDormitoryUsername(String username) {
        return dormitoryMapper.selectOne(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getUsername,username));
    }

    @Override
    public List<Dormitory> getDormitoryGender(String gender) {
        return dormitoryMapper.selectList(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getGender,gender));
    }

    @Override
    public List<Dormitory> getDormitoryDormBuilding(String dormBuilding) {
        return dormitoryMapper.selectList(new LambdaQueryWrapper<Dormitory>().eq(Dormitory::getDormBuilding,dormBuilding));
    }

    @Override
    public int addDormitory(Dormitory dormitory) {
        long dormitory_admin_id = idWorker.nextId();
        dormitory.setDormitoryAdminId(dormitory_admin_id);
        return dormitoryMapper.insert(dormitory);
    }

    @Override
    public boolean deleteDormitory(long dormitory_admin_id) {
        return this.removeById(dormitory_admin_id);
    }

    @Override
    @Transactional
    public int updateDormitory(Dormitory dormitory) {
        return dormitoryMapper.updateById(dormitory);
    }

    @Override
    public boolean updateDormitoryStatus(long dormitory_admin_id, boolean status) {
        LambdaUpdateWrapper<Dormitory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Dormitory::getDormitoryAdminId,dormitory_admin_id).set(Dormitory::getStatus,status);
        return dormitoryMapper.update(null, updateWrapper)>0;
            }


}

